<template>
  <div>
    <div class="adminInput">
      <div class="admin-in">
        <el-input
          v-model="input"
          @input="inputText"
          placeholder="请输入要查找的食物"
        ></el-input>
      </div>
    </div>

    <div>
      <el-table :data="tableData" v-loading="loading" style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="美食名称">
                <span>{{ props.row.fname }}</span>
              </el-form-item>
              <el-form-item label="美食图片">
                <img class="admin-foodimg" :src="props.row.fimg" alt="" />
              </el-form-item>
              <el-form-item label="美食 ID">
                <span>{{ props.row.fid }}</span>
              </el-form-item>
              <el-form-item label="浏览量">
                <span>{{ props.row.fvisits }}</span>
              </el-form-item>
              <el-form-item label="美食分类">
                <span>{{ props.row.ftName }}</span>
              </el-form-item>
              <el-form-item label="食物状态">
                <span>{{ props.row.fstate }}</span>
              </el-form-item>
              <el-form-item label="美食描述">
                <span>{{ props.row.fcontent }}</span>
              </el-form-item>
              <el-form-item label="制作步骤">
                <span v-html="props.row.fmake"> </span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="美食 ID" prop="fid"> </el-table-column>
        <el-table-column label="美食名称" prop="fname"> </el-table-column>
        <el-table-column label="美食类别" prop="ftName"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >修改状态</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="admin-fenye">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="total"
          :page-size="7"
          :currentPage="curPage"
          @current-change="page"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { request } from "../../network/request";

export default {
  name: "AdminFoods",
  data() {
    return {
      tableData: "",
      input: "",
      curPage: 1,
      total: 0,
      loading: true,
      state: "",
    };
  },
  created() {
    this.getMessage();
  },
  methods: {
    getMessage() {
      if (this.$route.query.page != undefined) {
        this.curPage = this.$route.query.page;
      }
      request({
        url: "/foot/queryAllFootBackstage",
        params: {
          pageNum: this.curPage,
        },
      })
        .then((res) => {
          this.tableData = res.data.data.list;
          this.total = res.data.data.total;
          this.loading = false;
          console.log(res);
        })
        .catch((err) => {
          console.log(err);
        });
    },

    handleEdit(index, row) {
      this.$router.push({
        path: "updatefoods",
        query: {
          fId: row.fid,
        },
      });
      console.log(index, row);
    },

    handleDelete(index, row) {
      if (row.fstate == 0) {
        this.state = 1;
      } else {
        this.state = 0;
      }
      request({
        url: "/foot/deletefootById",
        params: {
          fId: row.fid,
          fState: this.state,
        },
      })
        .then((res) => {
          console.log(res);
          this.$alert("修改完成", "提示", {
            confirmButtonText: "确定",
            callback: () => {
              this.getMessage();
            },
          });
        })
        .catch((err) => {
          console.log(err);
        });

      console.log(index, row);
    },

    page(currentPage) {
      this.curPage = currentPage;
      this.loading = true;
      this.$router.push({
        path: "adminfood",
        query: {
          page: currentPage,
        },
      });
      this.getMessage();
    },

    inputText() {
      this.curPage = 1;
      this.$router.push({
        path: "adminfood",
        query: {
          page: 1,
        },
      });
      request({
        url: "/foot/queryAllFootDtaLike",
        params: {
          foodName: this.input,
          pageNum: this.curPage,
        },
      })
        .then((res) => {
          this.tableData = res.data.data.list;
          this.total = res.data.data.total;
          this.loading = false;
          console.log(this.tableData);
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>

<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.admin-foodimg {
  width: 1.5rem;
  height: 1rem;
}
.adminUpadteFood {
  font-size: 0.1rem;
  margin-left: 0.8rem;
}

.adminUpdate-item {
  font-size: 0.1rem;
  display: flex;
  align-items: center;
  margin-top: 0.25rem;
}

.adminUpdate-item img {
  width: 1.2rem;
  height: 1rem;
}

.adminTextarea {
  width: 5rem;
}

.adminFood-name {
  width: 1rem;
}

.adminWanCheng {
  margin-left: 2.5rem;
  margin-top: 0.2rem;
}

.adminWanCheng button {
  width: 1.5rem;
}
</style>